package com.enqualcomm.kids.mvp.seetheworld;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ximalaya.ting.android.opensdk.model.category.Category;
import com.ximalaya.ting.android.opensdk.model.tag.Tag;

import java.util.List;

/**
 * Created by Sunzhigang on 2016/3/30.
 */
public class XmlyOldCategoryAdapter extends BaseAdapter {

    private Context context;

    private List<Category> categoryList;

    private int position;


    public XmlyOldCategoryAdapter(Context context) {
        this.context = context;
    }

    public void setCategoryData(List<Category> categoryList) {
        if (categoryList != null) {
            this.categoryList = categoryList;
            notifyDataSetChanged();
        }
    }

    public void setItemSelected(int position){
        this.position = position;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return categoryList == null ? 0 : categoryList.size();
    }

    @Override
    public Object getItem(int i) {
        return categoryList == null ? null : categoryList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.xmly_category_gl_item_, null);
            viewHolder.category_sdv = (SimpleDraweeView) view.findViewById(R.id.category_sdv);
            viewHolder.name_tv = (TextView) view.findViewById(R.id.name_tv);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        Uri uri = Uri.parse(categoryList.get(i).getCoverUrlSmall());
        viewHolder.category_sdv.setImageURI(uri);
        final long categoryID = categoryList.get(i).getId();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainCategoryClickListenr.OnMainCategoryClick(categoryID);
                setItemSelected(i);
            }
        });
        viewHolder.name_tv.setText(categoryList.get(i).getCategoryName());

        if (i == position){
            viewHolder.category_sdv.setSelected(true);
        }else {
            viewHolder.category_sdv.setSelected(false);
        }

        return view;
    }

    private static class ViewHolder {
        public SimpleDraweeView category_sdv;
        public TextView name_tv;
    }

    /**
     * 主分类
     */
    private OnMainCategoryClickListenr mainCategoryClickListenr;

    public void setOnMainCategoryClickListenr(OnMainCategoryClickListenr mainCategoryClickListenr) {
        this.mainCategoryClickListenr = mainCategoryClickListenr;
    }

    public interface OnMainCategoryClickListenr {
        void OnMainCategoryClick(long categoryid);
    }


}
